package clientView;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;

import object.Contract;
import utility.StringUtil;
import clientMain.Main;
import clientUtilGUI.UtilityGUI;
import database.ContractDelegate;


public class Contract_Update extends JDialog implements ActionListener{

	private JTextField txtContractName;
	private JLabel lblContName;
	private JLabel lblContractDes;
	private JTextArea txaContractDes;
	private JButton btnSave;
	private JButton btnCancel;
	private JScrollPane jCsro;
	private int id;
	private int a;
	public String name;
	public String des;
	boolean  flag = false;

	public Contract_Update(JFrame main,final String hD,final String mT,final int id1,int row)  {
		super(main, "Sửa Hợp Đồng", true);
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		a = row;
		id = id1;
		name = hD;
		des = mT;
		int height = 200;
	    int width = 420;
	    setSize(width, height);
	    setLocation((UtilityGUI.screenSize.width-width)/2, (UtilityGUI.screenSize.height-height)/2);
	    setResizable(false);
	    initComponet();
	}
		public void initComponet()
		{	
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{0, 0, 0};
		gridBagLayout.rowHeights = new int[]{0, 0, 0, 0};
		gridBagLayout.columnWeights = new double[]{0.0, 1.0, 0.0};
		gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 1.0};
		getContentPane().setLayout(gridBagLayout);
		
		lblContName = new JLabel("Hợp Đồng: ");
		lblContName.setFont(UtilityGUI.fontLblForm);
		GridBagConstraints gbc_lblConName = new GridBagConstraints();
		gbc_lblConName.anchor = GridBagConstraints.WEST;
		gbc_lblConName.insets = new Insets(15, 15, 5, 5);
		gbc_lblConName.fill = GridBagConstraints.VERTICAL;
		gbc_lblConName.gridx = 0;
		gbc_lblConName.gridy = 0;
		getContentPane().add(lblContName, gbc_lblConName);
		
		txtContractName = new JTextField();
		txtContractName.addFocusListener(new FocusAdapter() {
			public void focusLost(FocusEvent e){
				txtContractName.setText(StringUtil.removeUnwantedSpace(txtContractName.getText()));
			}
		});
		txtContractName.addFocusListener(new FocusListener() {
			@Override
			public void focusLost(FocusEvent e) {
				String a =  "  ";
				a = a.trim();
				if(txtContractName.getText().trim().equals(a)){
					txtContractName.setText(name);
					txtContractName.setBackground(UtilityGUI.colorBgOldTxtUpdate);
					txtContractName.setForeground(UtilityGUI.colorOldTxtUpdate);
				}
				
			}
			
			@Override
			public void focusGained(FocusEvent e) {
				if(txtContractName.getText().equals(name)){
				txtContractName.setBackground(UtilityGUI.colorBgNewTxtUpdate);
				txtContractName.setFont(UtilityGUI.fontTxtInsert);
				txtContractName.setText("");
				}
			}
		});
		//add event bat su kien khi viet de check trong database
		txtContractName.addCaretListener(new CaretListener() {
			public void caretUpdate(CaretEvent arg0) {
				ContractDelegate d = new ContractDelegate();
				if(!d.CheckInsertContractName((StringUtil.removeUnwantedSpace(txtContractName.getText())))&&txtContractName.getText().length()>3){
					btnSave.setEnabled(true);
				}else{
					btnSave.setEnabled(false);
					
				}
			
			}
		});
		GridBagConstraints gbc_textField = new GridBagConstraints();
		gbc_textField.gridwidth = 2;
		gbc_textField.insets = new Insets(15, 5, 5, 15);
		gbc_textField.fill = GridBagConstraints.HORIZONTAL;
		gbc_textField.gridx = 1;
		gbc_textField.gridy = 0;
		getContentPane().add(txtContractName, gbc_textField);
		txtContractName.setColumns(10);
		
		lblContractDes = new JLabel("Mô tả: ");
		lblContractDes.setFont(UtilityGUI.fontLblForm);
		GridBagConstraints gbc_lblConDes = new GridBagConstraints();
		gbc_lblConDes.anchor = GridBagConstraints.WEST;
		gbc_lblConDes.insets = new Insets(5, 15, 5, 5);
		gbc_lblConDes.fill = GridBagConstraints.VERTICAL;
		gbc_lblConDes.gridx = 0;
		gbc_lblConDes.gridy = 1;
		getContentPane().add(lblContractDes, gbc_lblConDes);
		
		txaContractDes = new JTextArea(4,10);
		txaContractDes.setBackground(UtilityGUI.colorBgOldTxtUpdate);
		txaContractDes.setForeground(UtilityGUI.colorOldTxtUpdate);
		txaContractDes.setFont(UtilityGUI.fontTxtInsert);
		txaContractDes.setText(des);
		txaContractDes.addFocusListener(new FocusAdapter() {
			public void focusLost(FocusEvent e){
				txaContractDes.setText(StringUtil.removeUnwantedSpace(txaContractDes.getText()));
			}
		});
		txaContractDes.addFocusListener(new FocusListener() {
			@Override
			public void focusLost(FocusEvent e) {
				String a =  "  ";
				a = a.trim();
				if(txaContractDes.getText().trim().equals(a)){
					txaContractDes.setText(des);
					txaContractDes.setBackground(UtilityGUI.colorBgOldTxtUpdate);
					txaContractDes.setForeground(UtilityGUI.colorOldTxtUpdate);
				}
			}			
			@Override
			public void focusGained(FocusEvent e) { 
				if(txaContractDes.getText().equals(des)){
				txaContractDes.setBackground(UtilityGUI.colorBgNewTxtUpdate);
				txaContractDes.setFont(UtilityGUI.fontTxtInsert);
				txaContractDes.setText("");
				}
			}
		});
		jCsro = new JScrollPane(txaContractDes);
		GridBagConstraints gbc_textArea = new GridBagConstraints();
		gbc_textArea.gridwidth = 2;
		gbc_textArea.insets = new Insets(5, 5, 5, 15);
		gbc_textArea.fill = GridBagConstraints.BOTH;
		gbc_textArea.gridx = 1;
		gbc_textArea.gridy = 1;
		getContentPane().add(jCsro, gbc_textArea);
		
		btnSave = new JButton("Lưu");
		btnSave.setEnabled(false);
		btnSave.setFont(UtilityGUI.fontBtnForm);
		GridBagConstraints gbc_btnSave = new GridBagConstraints();
		gbc_btnSave.fill = GridBagConstraints.VERTICAL;
		gbc_btnSave.anchor = GridBagConstraints.EAST;
		gbc_btnSave.insets = new Insets(10, 0, 5, 5);
		gbc_btnSave.gridx = 1;
		gbc_btnSave.gridy = 2;
		getContentPane().add(btnSave, gbc_btnSave);
		btnSave.addActionListener(this);
		
		btnCancel = new JButton("Thoát");
		btnCancel.setFont(UtilityGUI.fontBtnForm);
		GridBagConstraints gbc_btnCancel = new GridBagConstraints();
		gbc_btnCancel.fill = GridBagConstraints.BOTH;
		gbc_btnCancel.insets = new Insets(10, 0, 5, 15);
		gbc_btnCancel.gridx = 2;
		gbc_btnCancel.gridy = 2;
		getContentPane().add(btnCancel, gbc_btnCancel);
		btnCancel.addActionListener(this);
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==btnSave){	
			Contract con = new Contract();
			con.setContractId(id);
			con.setContractName(StringUtil.removeUnwantedSpace(txtContractName.getText()));
			con.setContractDes(StringUtil.removeUnwantedSpace(txaContractDes.getText()));
			ContractDelegate p = new ContractDelegate();
		    Boolean flag = p.updateContract(con, Main.userId);
				 if(flag == true){
				    	            this.setVisible(false);
				    				JOptionPane.showMessageDialog(null, UtilityGUI.successAction, 
									UtilityGUI.titleAlert, JOptionPane.INFORMATION_MESSAGE);
						            Contract_View.tblContract.getModel().setValueAt(StringUtil.removeUnwantedSpace(txtContractName.getText()), a, 1) ;
						            Contract_View.tblContract.getModel().setValueAt(StringUtil.removeUnwantedSpace(txaContractDes.getText()), a, 2) ;
				  }else{
					    	 		JOptionPane.showMessageDialog(null, UtilityGUI.errorAction ,
									UtilityGUI.titleAlert, JOptionPane.ERROR_MESSAGE);
				    }
		}else if(e.getSource()==btnCancel){
			dispose();
		}
}



}
